package com.example.dell.newstart.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.dell.newstart.R;
import com.example.dell.newstart.base.BaseFragment;
import com.example.dell.newstart.base.RequestInterface;
import com.example.dell.newstart.base.ServerResponse;
import com.example.dell.newstart.bean.MyPageInfo;
import com.example.dell.newstart.bean.Product;
import com.example.dell.newstart.search.CustomSearchView;
import com.example.dell.newstart.type.ProductListActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * created by Dongkai on 2018/12/25
 */

public class HomeFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private CustomSearchView customSearchView;
    private SmartRefreshLayout smartRefreshLayout;
  private  RecyclerView rlvSessions;
    private List<Product> productList=new ArrayList<>();
   // private List<Product> mgood;

    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        recyclerView = getContentView().findViewById(R.id.rv_home);
        customSearchView = getContentView().findViewById(R.id.csv_home);


    }

    @Override
    protected void initData() {

    }

    @Override

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        HomeAdapter adapter = new HomeAdapter(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);


        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);

    }

    private void getProductList() {
        Retrofit retrofit;
        retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.getProductsList2 getProductsList2 = retrofit.create(RequestInterface.getProductsList2.class);
        Call<ServerResponse<MyPageInfo<Product>>> call = getProductsList2.getProducts("", 10, 1, 10, "");

        call.enqueue(new Callback<ServerResponse<MyPageInfo<Product>>>() {

            @Override
            public void onResponse(Call<ServerResponse<MyPageInfo<Product>>> call, Response<ServerResponse<MyPageInfo<Product>>> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                if (response.body() == null) {
                    toastShort("未获取到相关数据");
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    MyPageInfo myPageInfo = response.body().getData();
                    productList = myPageInfo.getList();
                } else {
                    toastShort("登陆失败");
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<MyPageInfo<Product>>> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                toastShort("登陆失败！");
            }
        });
    }


    public void onSearch(String keywords) {
        Intent intent=new Intent(getContext(),ProductListActivity.class);
        intent.putExtra("keywords",keywords);
        startActivity(intent);
    }
}
